package gui.undo;

import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.undo.CannotRedoException;
import javax.swing.undo.UndoManager;

@SuppressWarnings("serial")
public class RedoAction extends AbstractAction {
	private UndoManager undo = null;
	private UndoAction undoAction = null;

	public RedoAction(UndoManager undo, UndoAction undoAction) {
		super("Redo");
		this.undo = undo;
		this.undoAction = undoAction;
		setEnabled(false);
	}

	public void actionPerformed(ActionEvent e) {
		try {
			undo.redo();
		} catch (CannotRedoException ex) {
			System.out.println("Unable to redo: " + ex);
			ex.printStackTrace();
		}
		updateRedoState();
		undoAction.updateUndoState();
	}

	public void updateRedoState() {
		if (undo.canRedo()) {
			setEnabled(true);
			putValue(Action.NAME, undo.getRedoPresentationName());
		} else {
			setEnabled(false);
			putValue(Action.NAME, "Redo");
		}
	}
}